import 'package:basic_library/src/widgets/view/text_center_bottom_line.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

///
/// 弹出底部列表 - 单选
///
class QListMenuSheet extends Dialog {
  final BuildContext buildContext;
  final int? index;
  final List<ItemDataModel> pickerChildren;
  final Function? sureCallBack;

  const QListMenuSheet({
    Key? key,
    required this.buildContext,
    required this.pickerChildren,
    this.index,
    this.sureCallBack,
  }) : super(key: key);

  /// 取消对话框
  static hide(BuildContext context) {
    Navigator.of(context).pop();
  }

  /// 显示对话框
  void show() {
    showCupertinoModalPopup(
      context: buildContext,
      builder: (BuildContext context1) {
        return ListContentView(
          index: index,
          pickerChildren: pickerChildren,
          sureCallBack: sureCallBack,
        );
      },
    );
  }
}

class ListContentView extends StatefulWidget {
  final int? index;
  final List<ItemDataModel> pickerChildren;
  final Function? sureCallBack;

  const ListContentView({
    Key? key,
    required this.pickerChildren,
    this.index,
    this.sureCallBack,
  }) : super(key: key);

  @override
  _ListContentViewState createState() => _ListContentViewState();
}

class _ListContentViewState extends State<ListContentView> {
  _items() {
    return widget.pickerChildren.map((e) {
      return TextCenterBottomLineCell(
        title: e.name,
        isSelect: widget.index == e.state,
        tapCallback: () {
          Navigator.pop(context);
          if (widget.sureCallBack != null) {
            widget.sureCallBack!(e);
          }
        },
      );
    }).toList();
  }

  @override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height * 0.55;
    return widget.pickerChildren.length > 5
        ? Container(
            color: const Color(0xFFF1F3F7),
            height: height,
            child: Stack(
              children: [
                Container(
                  margin: const EdgeInsets.only(bottom: 158 / 2),
                  child: ListView.builder(
                    padding: const EdgeInsets.all(0),
                    itemCount: widget.pickerChildren.length,
                    itemBuilder: (context, index) {
                      ItemDataModel model = widget.pickerChildren[index];
                      return TextCenterBottomLineCell(
                        title: model.name,
                        isSelect: widget.index == model.state,
                        tapCallback: () {
                          Navigator.pop(context);
                          if (widget.sureCallBack != null) {
                            widget.sureCallBack!(model);
                          }
                        },
                      );
                    },
                  ),
                ),
                Positioned(
                  left: 0,
                  right: 0,
                  bottom: 0,
                  child: TextCenterBottomLineCell(
                    isCancel: true,
                    title: "取消",
                    tapCallback: () {
                      Navigator.pop(context);
                    },
                  ),
                )
              ],
            ),
          )
        : Container(
            color: const Color(0xFFF1F3F7),
            child: Stack(
              children: [
                Padding(
                  padding: const EdgeInsets.only(bottom: 158 / 2),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: _items(),
                  ),
                ),
                Positioned(
                  left: 0,
                  right: 0,
                  bottom: 0,
                  child: TextCenterBottomLineCell(
                    isCancel: true,
                    title: "取消",
                    tapCallback: () {
                      Navigator.pop(context);
                    },
                  ),
                )
              ],
            ),
          );
  }
}

class ItemDataModel {
  final String name;
  final int state;
  bool select;

  ItemDataModel({required this.name, required this.state, this.select = false});

  @override
  String toString() {
    // TODO: implement toString
    return "{name:$name, state:$state, select:$select}";
  }
}
